﻿using Books.Data;
using Books.Data.Model;
using Books.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Books.Adapters
{
    public class BookDataAdapter : IBookAdapter
    {
        public List<BookViewModel> GetBooks(string userId)
        {
            List<BookViewModel> models = null;
            using (BooksDbContext db = new BooksDbContext())
            {
                // for every Book in the database, I want you to create a Book view model for it.
                models = db.Books
                        //.Where(m => m.UserId == userId)
                        .Select(m => new BookViewModel
                        {
                            BookId = m.BookId,
                            Author = m.Author,
                            BookImage = m.BookImage,
                            Rating = m.Rating,
                            Title = m.Title
                        }).ToList();

            }
            return models;

        }
        public void CreateBook(BookViewModel model, string userId)
        {
            using (BooksDbContext db = new BooksDbContext())
            {
                Book dbBook = new Book
                {
                    BookImage = model.BookImage,
                    Rating = model.Rating,
                    Title = model.Title,
                    UserId = userId

                };
                db.Books.Add(dbBook);
                //This actually saves the changes. This is bringing your groceries back from the store and actually saving them.
                db.SaveChanges();
            }

        }

        public BookViewModel GetBook(int BookId)
        {
            throw new NotImplementedException();
        }

        public void Update(BookViewModel model)
        {
            throw new NotImplementedException();
        }
    }
}